<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="教师编号" prop="teacherId">
        <el-input
          v-model="queryParams.teacherId"
          placeholder="请输入教师编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实验室编号" prop="labId">
        <el-input
          v-model="queryParams.labId"
          placeholder="请输入实验室编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报修日期" prop="repairDate">
        <el-date-picker clearable
          v-model="queryParams.repairDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择报修日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="实验员编号" prop="experimenterId">
        <el-input
          v-model="queryParams.experimenterId"
          placeholder="请输入实验员编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报修状态" prop="repairStatus">
        <el-select v-model="queryParams.repairStatus" placeholder="请选择报修状态" clearable>
          <el-option
            v-for="dict in dict.type.jwxt_repair_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['jwxt:maintenance:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['jwxt:maintenance:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['jwxt:maintenance:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['jwxt:maintenance:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="maintenanceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="报修编号" align="center" prop="repairId" />
      <el-table-column label="教师编号" align="center" prop="teacherId" />
      <el-table-column label="实验室编号" align="center" prop="labId" />
      <el-table-column label="故障描述" align="center" prop="faultDescription">
        <template slot-scope="scope">
          <el-popover
            placement="top-start"
            title="故障描述"
            width="400"
            trigger="hover"
            :content="scope.row.faultDescription">
            <el-input
              slot="reference"
              type="textarea"
              :autosize="{ minRows: 2, maxRows: 4}"
              readonly
              v-if="scope.row.faultDescription!=null"
              v-model="scope.row.faultDescription">
            </el-input>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="报修日期" align="center" prop="repairDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.repairDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="负责实验员" align="center" prop="experimenterId" />
      <el-table-column label="报修状态" align="center" prop="repairStatus">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="success"
            v-if="scope.row.repairStatus=='已维修'"
          >{{ scope.row.repairStatus }}</el-button>
          <el-button
            size="mini"
            type="danger"
            v-if="scope.row.repairStatus=='未维修'"
          >{{ scope.row.repairStatus }}</el-button>
          <el-button
            size="mini"
            type="warning"
            v-if="scope.row.repairStatus=='维修中'"
          >{{ scope.row.repairStatus }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="维修说明" align="center" prop="repairDescription" >
        <template slot-scope="scope">
          <el-popover
            placement="top-start"
            title="维修描述"
            width="400"
            trigger="hover"
            :content="scope.row.repairDescription">
            <el-input
              slot="reference"
              type="textarea"
              :autosize="{ minRows: 2, maxRows: 4}"
              readonly
              v-if="scope.row.repairDescription!=null"
              v-model="scope.row.repairDescription">
            </el-input>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-if="scope.row.repairStatus!='已维修'"
            v-hasPermi="['jwxt:maintenance:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['jwxt:maintenance:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-chat-line-round"
            @click="handleVerify(scope.row);"
            v-if="scope.row.repairStatus!='已维修'"
            v-hasPermi="['jwxt:maintenance:verify']"
          >审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备报修对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="教师编号" prop="teacherId">
          <el-select v-model="form.teacherId" filterable placeholder="请选择教师编号">
            <el-option
              v-for="item in this.teaIdOption"
              :key="item.userId"
              :label="`${item.userId} - ${item.userName}`"
              :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="实验室编号" prop="labId">
          <el-select v-model="form.labId" filterable placeholder="请选择实验室编号">
            <el-option
              v-for="item in LabIdOption"
              :key="item.labId"
              :label="`${item.labName}${item.labNumber}`"
              :value="item.labId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="故障描述" prop="faultDescription">
          <el-input v-model="form.faultDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

<!--    审核设备报修状态对话框-->
    <el-dialog :title="title" :visible.sync="verifyDialog" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="报修状态" prop="repairStatus">
          <el-select v-model="form.repairStatus" placeholder="请选择报修状态" clearable>
            <el-option
              v-for="dict in dict.type.jwxt_repair_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="维修说明" prop="repairDescription">
          <el-input v-model="form.repairDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listMaintenance, getMaintenance, delMaintenance, addMaintenance, updateMaintenance, getTeaIdData, getLabIdData } from "@/api/jwxt/maintenance";

export default {
  name: "Maintenance",
  dicts: ['jwxt_repair_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备报修表格数据
      maintenanceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      verifyDialog:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teacherId: null,
        labId: null,
        faultDescription: null,
        repairDate: null,
        experimenterId: null,
        repairStatus: null,
        repairDescription: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        teacherId: [{ required: true, message: '该项为必填项', trigger: 'blur' }],
        labId:[{ required: true, message: '该项为必填项', trigger: 'blur' }]
      },
      // 教师编号动态下拉框数据
      teaIdOption: {
      },
      // 实验室编号动态下拉框数据
      LabIdOption: {
      }
    };
  },
  created() {
    this.getList();
    this.fillTeaIdOption();
    this.fillLabIdOption();
  },
  methods: {
    /** 查询设备报修列表 */
    getList() {
      this.loading = true;
      listMaintenance(this.queryParams).then(response => {
        this.maintenanceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.verifyDialog=false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        repairId: null,
        teacherId: null,
        labId: null,
        faultDescription: null,
        repairDate: null,
        experimenterId: null,
        repairStatus: null,
        repairDescription: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.repairId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.fillTeaIdOption();
      this.fillLabIdOption();
      this.reset();
      this.open = true;
      this.title = "添加设备报修";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.fillTeaIdOption();
      this.fillLabIdOption();
      this.reset();
      const repairId = row.repairId || this.ids
      getMaintenance(repairId).then(response => {
        if(response.data.repairStatus=="已维修"){
          this.$modal.msgError("该报修已完成！无法修改！");
          return;
        }
        this.form = response.data;
        this.open = true;
        this.title = "修改设备报修";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.repairId != null) {
            updateMaintenance(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.verifyDialog=false;
              this.getList();
            });
          } else {
            addMaintenance(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const repairIds = row.repairId || this.ids;
      this.$modal.confirm('是否确认删除设备报修编号为"' + repairIds + '"的数据项？').then(function() {
        return delMaintenance(repairIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('jwxt/maintenance/export', {
        ...this.queryParams
      }, `maintenance_${new Date().getTime()}.xlsx`)
    },
    /*审核按钮操作*/
    handleVerify(row){
      this.reset();
      const repairId = row.repairId || this.ids
      getMaintenance(repairId).then(response => {
        if(response.data.repairStatus=="已维修"){
          this.$modal.msgError("该报修已完成！无法修改！");
          return;
        }
        this.form = response.data;
        this.verifyDialog = true;
        this.title = "审核设备报修";
      });
    },
    // 填充教师到相应下拉框数据中
    fillTeaIdOption(){
      getTeaIdData().then(respon=>{
        this.teaIdOption=respon.data;
      })
    },
    // 填充实验室到相应下拉框数据中
    fillLabIdOption(){
      getLabIdData().then(respon=>{
        this.LabIdOption=respon.data;
      })
    }

    //END
  }
};
</script>
